/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.fs;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import com.nhncorp.neptune.changelog.ChangeLogClient;
import com.nhncorp.neptune.common.conf.NConfiguration;
import com.nhncorp.neptune.fs.PipeBasedChangeLogFileSystem;
import com.nhncorp.neptune.fs.PipeBasedChangeLogFileSystem.ChangeLogFileSystemInfo;
import com.nhncorp.neptune.neptune_test.common.MiniChangeLogCluster;

public class TestPipeBasedChangeLogFileSystem extends TestCase {
  MiniChangeLogCluster cluster;
  NConfiguration conf;
  protected void setUp() throws Exception {
    conf = new NConfiguration();
    conf.set("neptune.changelog.filesystem", "pipe");
    conf.set("neptune.filesystem", "local");
    conf.set("neptune.local.temp", System.getProperty("user.home") + File.separator + ".neptune_local");

    cluster = MiniChangeLogCluster.create(conf);
  }

  protected void tearDown() throws Exception {
    cluster.shutdown();
  }
  
  public void testConsistencyCheck() {
    final String TABLET_NAME = "UNIT_TEST_TABLET";
    
    PipeBasedChangeLogFileSystem pfs = new PipeBasedChangeLogFileSystem(true);
    try {
      pfs.init(null, new NConfiguration(), null, null);

      ChangeLogClient client = pfs.getFromCache(TABLET_NAME);
      ChangeLogFileSystemInfo fsInfo = PipeBasedChangeLogFileSystem.fsCache.get(TABLET_NAME);
      assertEquals(1, fsInfo.numOfUsedPipes);
      assertTrue(client.getConsistencyCheck());
      
      ChangeLogClient client2 = pfs.getFromCache(TABLET_NAME);
      assertEquals(2, fsInfo.numOfUsedPipes);
      assertFalse(client2.getConsistencyCheck());
      
      ChangeLogClient client3 = pfs.getFromCache(TABLET_NAME);
      assertEquals(3, fsInfo.numOfUsedPipes);
      assertFalse(client3.getConsistencyCheck());
      
      pfs.returnToCache(TABLET_NAME, client3);
      assertEquals(2, fsInfo.numOfUsedPipes);
      
      pfs.returnToCache(TABLET_NAME, client2);
      assertEquals(1, fsInfo.numOfUsedPipes);

      client2 = pfs.getFromCache(TABLET_NAME);
      assertEquals(2, fsInfo.numOfUsedPipes);
      assertFalse(client2.getConsistencyCheck());

      pfs.returnToCache(TABLET_NAME, client2);
      assertEquals(1, fsInfo.numOfUsedPipes);

      pfs.returnToCache(TABLET_NAME, client);
      assertEquals(0, fsInfo.numOfUsedPipes);
      
      client = pfs.getFromCache(TABLET_NAME);
      assertEquals(1, fsInfo.numOfUsedPipes);
      assertTrue(client.getConsistencyCheck());
      
      pfs.returnToCache(TABLET_NAME, client);
      assertEquals(0, fsInfo.numOfUsedPipes);
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  }
}
